<template>
  <view class="content">
    <view class="order-box">
      <view class="search-box">
        <!-- 设置圆角 -->
        <uni-search-bar :radius="100" @confirm="search" v-model="keyword"></uni-search-bar>
      </view>
      <view class="order-list-box">
        <view class="order-tba">
          <text :class="dingdanIndex == 0?'active':''" @click="ornav2(0)">全部订单</text>
          <text :class="dingdanIndex == 1?'active':''" @click="ornav2(1)">待付款</text>
          <text :class="dingdanIndex == 2?'active':''" @click="ornav2(2)">待收货</text>
          <text :class="dingdanIndex == 3?'active':''" @click="ornav2(3)">售后</text>
        </view>
        <view class="order-list">
          <view class="order-dh" v-for="(item,index) in dingdanList" :key="index">
            <view class="order-dh-n1">
              <view>订单号：{{item.order_no}}</view>
              <text v-if="item.status == -1">交易关闭</text>
              <text v-if="item.pay_status == 0&item.status == 0">待付款</text>
              <text
                v-if="item.pay_status == 1&item.status == 1&item.refund.length == 0"
              >待配送</text>
              <text
                v-if="item.pay_status == 1&item.status == 2&item.refund.length == 0"
              >配送中</text>
              <text
                v-if="item.pay_status == 1&item.status == 1&item.refund.length > 0"
              >{{item.refund[0].status==0?'处理中':'已处理'}}</text>
              <text
                v-if="item.pay_status == 1&item.status == 2&item.refund.length > 0"
              >{{item.refund[0].status==0?'处理中':'已处理'}}</text>
              <text v-if="item.pay_status == 1&item.status == 3">已完成</text>
            </view>

            <view class="order-dh-n2" v-if="item.items[0].detail.goods">
              <!--   -->
              <view v-for="(text,num) in item.items" :key="num">
                <span v-for="(text,num) in item.items[num].detail.goods" :key="num">
                  <span v-if="num != 0">+</span>
                  {{text.title}}{{text.attr_name}}
                </span>
              </view>

              <view class="view">
                共
                <text>{{item.zong}}</text>件商品
              </view>
            </view>

            <view class="order-dh-n2" v-else>
              <!--   -->
              <view v-for="(detail,i) in item.items" :key="i">
                <span>{{detail.detail.title}} x {{detail.num}}</span>
                <span>￥{{detail.detail.discount_price * detail.num}}</span>
              </view>
              <view class="view">
                共
                <text>{{item.zong}}</text>件商品
              </view>
            </view>

            <view class="order-dh-n3">
              <view class="order-dh-left">
                <view>
                  合计￥
                  <text>{{item.pay_amount}}</text>
                </view>
              </view>
              <view class="order-dh-right">
                <button type="default" @click="xiangqing(item.pay_status,item.status,item.id)">订单详情</button>
                <button
                  v-if="item.pay_status == 0&item.status == 0"
                  type="default"
                  class="fk-btn"
                  @click="ljfkck(item.id,item.type)"
                >立即付款</button>
                <button
                  v-if="item.pay_status == 1&(item.status == 1||item.status == 2)&item.refund.length == 0"
                  type="default"
                  class="shh-btn"
                  @click="shenqck(item.id)"
                >申请售后</button>
                <button
                  v-if="item.refund.length != 0"
                  type="default"
                  @click="shhxqing(item.id)"
                >售后详情</button>
              </view>
            </view>
          </view>
        </view>
      </view>
    </view>
    <dacizinavtag :current="item_index" @click="tabClick"></dacizinavtag>
  </view>
</template>

<script>
import dacizinavtag from "@/components/dacizi-navtag/dacizi-navtag.vue";

export default {
  components: {
    dacizinavtag
  },
  data() {
    return {
      dingdanList: [],
      zong: "",
      dingdanIndex: 0,
      item_index: 3, // 单页面id
      page: 1,
      psize: 99999,
      status: "",
      keyword: ""
    };
  },
  onLoad(num) {
    if (num.num == 1) {
      this.status = 0;
      this.ornav2(num.num);
    } else if (num.num == 3) {
      this.status = "";
      this.ornav2(num.num);
    } else {
      this.getdingdan();
    }
  },
  onShow() {
    if (this.dingdanIndex == 0) {
      this.ornav2(0);
    } else if (this.dingdanIndex == 1) {
      this.ornav2(1);
    } else if (this.dingdanIndex == 2) {
      this.ornav2(2);
    } else if (this.dingdanIndex == 3) {
      this.ornav2(3);
    }
  },
  methods: {
    getdingdan() {
      var than = this;
      //获取订单列表
      uni.request({
        url: getApp().globalData.http + "/order/getList",
        data: {
          page: this.page,
          psize: this.psize,
          status: this.status,
          keyword: this.keyword
        },
        method: "post",
        header: {
          "content-type": "application/x-www-form-urlencoded",
          token: uni.getStorageSync("token")
        },
        success: res => {
          if (res.data.code == 1) {
            than.dingdanList = res.data.data.data;
            console.log(res.data.data.data);
            than.dingdanList.forEach(e => {
              e.zong = 0;
              e.items.forEach(i => {
                e.zong += i.num;
              });
            });
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: "none",
              duration: 2000
            });
          }
        }
      });
    },

    search() {
      this.getdingdan();
    },
    //全部订单
    ornav() {
      wx.reLaunch({
        url: "/pages/my/order"
      });
    },
    //待付款
    ornav2(index) {
      this.dingdanIndex = index;
      if (index == 0) {
        this.status = "";
        this.getdingdan();
      } else if (index == 1) {
        this.status = 0;
        this.getdingdan();
      } else if (index == 2) {
        this.status = 1;
        this.getdingdan();
      } else if (index == 3) {
        var than = this;
        //获取订单列表
        uni.request({
          url: getApp().globalData.http + "/order/refundOrder",
          data: {
            page: this.page,
            psize: this.psize
          },
          method: "post",
          header: {
            "content-type": "application/x-www-form-urlencoded",
            token: uni.getStorageSync("token")
          },
          success: res => {
            than.dingdanList = res.data.data.data;
            than.dingdanList.forEach((e,k) => {
              var num = e.refund.status
              e.refund = []
              e.refund.push({"status":num})
              e.zong = 0;
              e.items.forEach(i => {
                e.zong += i.num;
              });
            });
          }
        });
      }

      // wx.reLaunch({
      //   url: "/pages/my/order2"
      // });
    },
    //待收货
    ornav3() {
      wx.reLaunch({
        url: "/pages/my/order3"
      });
    },
    //售后
    ornav4() {
      wx.reLaunch({
        url: "/pages/my/order4"
      });
    },

    xiangqing(num, num1, id) {
      if ((num == 0) & (num1 != -1)) {
        this.daickxqing(id);
      } else {
        this.ckxqing(id);
      }
    },

    //订单详情
    ckxqing(id) {
      wx.navigateTo({
        url: "/pages/my/order-info?id=" + id
      });
    },
    //待付款订单详情
    daickxqing(id) {
      wx.navigateTo({
        url: "/pages/my/daizhifu?id=" + id
      });
    },
    //立即付款
    ljfkck(id, type) {
      uni.request({
        url: getApp().globalData.http + "/order/pay",
        data: {
          id: id
        },
        method: "post",
        header: {
          "content-type": "application/x-www-form-urlencoded",
          token: uni.getStorageSync("token")
        },
        success: res => {
          if (res.data.code) {
            wx.requestPayment({
              timeStamp: res.data.data.timeStamp,
              nonceStr: res.data.data.nonceStr,
              package: res.data.data.package,
              signType: res.data.data.signType,
              paySign: res.data.data.paySign,
              success(res) {
                if (type == 1) {
                  wx.navigateTo({
                    url: "/pages/my/zhifu"
                  });
                } else {
                  wx.navigateTo({
                    url: "/pages/my/cfzhifu?id=" + id
                  });
                }
              },
              fail(res) {}
            });
          } else {
            wx.showToast({
              title: res.data.msg,
              icon: "none",
              duration: 2000
            });
          }
        }
      });
    },
    //申请售后
    shenqck(id) {
      wx.navigateTo({
        url: "/pages/my/after-sales2?id=" + id
      });
    },
    //售后详情
    shhxqing(id) {
      wx.navigateTo({
        url: "/pages/my/after-sales-info?id=" + id
      });
    }
  }
};
</script>
